<?php
/**
 * Created by PhpStorm.
 * User: Marmot
 * Date: 2018/5/8
 * Time: 13:56
 */
namespace app\admin\controller\common;
use app\admin\model\Auth;


/**权限类
 * Class Power
 * @package app\admin\controller\common
 */
class Power {
    public $auth_data = '';//权限数据
    protected $uid = '';//用户ID
    protected $to_id = '';//被设置权限的人的ID
    protected $to_level = '';//被设置权限的人的权限等级

    /**构造函数
     * Power constructor.
     * @param string $id 权限id
     */
    public function __construct($id = '')
    {
        //初始化
        if ($id==''){
            $id = session('login.auth');
            $this->uid = $id;
        }
        $auth = new Auth();
        $auth_arr =$auth -> table('d_auth')->where('id='.$id)->json(['auth_res'])->find()->toArray();
        $this->auth_data = $auth_arr;
    }
//-----------------------------------------------------查询方法---------------------------------------------------------
    /**获取权限等级
     * @return string 获取权限等级
     */
    public function get_auth_level(){
        return $this->auth_data['auth_level'];
    }

    /**获取功能权限
     * @return mixed 返回的是功能权限数组
     */
    public function get_auth_fun(){
        $auth = new Auth();
        $auth_fun = $auth->get_auth_role('id='.$this->auth_data['auth_fun'],false);
        foreach ($auth_fun[0]['role_res'] as $k => &$v){
            $v = $auth->table('d_nav')->field('id,nav_url,nav_parent')->where('id='.$v)->find();
        }
        return $auth_fun[0];
    }

    public function get_auth_funs($auth_fun){
        $auth = new Auth();
        $arr = [];
        foreach ($auth_fun as $k => $v){
            $auth_fun = $auth->get_auth_role('id='.$v,false);
            foreach ($auth_fun[0]['role_res'] as $k => &$vo){
                $vo = $auth->table('d_nav')->field('id,nav_url,nav_parent')->where('id='.$vo)->find();
                if ($vo != null) {
                    $vo = $vo->toArray();
                } else {
                    unset($auth_fun[0]['role_res'][$k]);
                }
            }
            $arr[] = $auth_fun[0];
        }
        return $arr;
    }
    /**获取资源权限
     * @return array 返回的是权限资源数组
     */
    public function get_auth_res(){
        $auth = new Auth();
        $auth_res = [];
        foreach ($this->auth_data['auth_res'] as $v){
            $auth_res[] = $auth->table('d_auth_res')->where('id='.$v)->json(['res'])->select()->toArray();
        }
        return $auth_res;
    }

    /**获取权限类属性
     * @param string $name  属性名
     * @param string $value 属性值
     * @return string 有属性值则是设置，没有则是获取
     */
    public function auth_attr($name = '',$value = ''){
        if ($value != ''){
            return $this->$name = $value;
        }else{
            return $this->$name;
        }
    }

    /**返回一个功能权限的ID数组
     * @return array
     */
    public function get_auth_fun_id_arr(){
        $auth_fun = json_decode($this->auth_data['auth_fun']);
        $where = [];
        $where[] = ['is_show','=','1'];
        if (is_array($auth_fun)){
            $arr = $this->get_auth_funs($auth_fun);
            foreach ($arr as $k => $v){
                foreach ( $v['role_res'] as $vo){
                    $fun_arr[] = $vo['id'];
                }
            }
        }else{
            $auth_fun = $this->get_auth_fun();
            foreach ( $auth_fun['role_res'] as $v){
                $fun_arr[] = $v['id'];
            }
        }
        $where[] = ['id','in',$fun_arr];
        return $where;
    }


    /**返回权限模板
     * @param string $wh
     * @return array|bool
     */
    public function get_auth_model($wh=''){
        $auth = new Auth();
        $where[] = ['auth_level','>=',$this->auth_data['auth_level']];
        $res = $auth->table('d_auth')
            ->json(['auth_fun'])
            ->where($where)
            ->where($wh)
            ->select()
            ->toArray();
        if(!empty($res)){
            foreach($res as $k => &$v){
                if (!empty($v['auth_fun']) && is_array($v['auth_fun'])){
                    foreach ($v['auth_fun'] as $ke =>$vo){
                        $v['auth_fun'][$ke] = $auth->table('d_auth_fun_role')->field('role_name,id')->where('id='.$vo)->find()->toArray();
                    }
                }
            }
        }
        if ($res){
            return $res;
        }else{
            return false;
        }
    }
//-----------------------------------------------------设置方法---------------------------------------------------------

    /**设置权限等级
     * @param string $level
     * @return bool
     */
    public function set_auth_level($level=''){
        if ($this->to_level > $this->auth_data['auth_level'] and $level != ''){
            $this->auth_data['level'] = $level;
            return true;
        }
        return false;
    }

    /**返回一个自身的对象，可用于对比、设置不同人的权限
     * @param string $to_id
     * @return Power
     */
//    public function set_to_id($to_id=''){
//        $this->to_id = $to_id;
//        return new Power($to_id);
//    }

    /**设置或者更新权限
     * @param string $data
     * @param string $id
     * @return bool
     */
    public function set_auth_model($data='',$id='')
    {
        if ($data == ''){
            $data = $this->auth_data;
        }
        if (empty($data['copy_data'])){
            $data['auth_res'] = 0;
        }
        if ($data != '') {
            $auth = new Auth();
            if (empty($data['auth_res'])) {
                $data['auth_res'] = '';
            }
            if ($id != '') {
                if ($data['auth_res']) {
                    $res = $auth->table('d_auth')->json(['auth_fun'])->json(['auth_res'])->where('id=' . $id)->update($data);
                } else {
                    $res = $auth->table('d_auth')->json(['auth_fun'])->where('id=' . $id)->update($data);
                }
            }else{
                if ($data['auth_res']) {
                    $res = $auth->table('d_auth')->json(['auth_fun'])->json(['auth_res'])->insertGetId($data);
                } else {
                    $res = $auth->table('d_auth')->json(['auth_fun'])->insertGetId($data);
                }
            }
            if ($res) {
                    return $res;
                }
        }
        return false;
    }

    /**注册时设置权限以及初始化
     * @param $auth 权限模板ID
     * @param string $name 权限模板名称
     * @return bool 设置成功则返回新增权限数据的ID 否则返回false；
     */
    public function set_auth($auth,$name=""){
        //查询权限模板
        $auth_model = $this->get_auth_model($auth);
        //用模板数据给新增用户增加一条用户权限数据
        $auth_model = $auth_model[0];
        unset($auth_model['id']);
        $auth_model['auth_model'] = 0;
        $auth_model['auth_name'] = $name;
        $arr = [];
        if (empty($auth_model['copy_data'])){
            $auth_model['auth_res']=0;
        }else{
            //这里set资源权限

        }
        //这里set功能权限
        foreach ($auth_model['auth_fun'] as $k => $v){
            $arr[] = $v['id'];
        }
        $auth_model['auth_fun'] = $arr;
        $r = $this->set_auth_model($auth_model);
        return $r;
    }

    /**
     *设置资源权限
     *
     *
     */
    public function set_auth_res(){

    }
//-----------------------------------------------------验证方法---------------------------------------------------------

    /** 功能权限验证
     * @return bool
     */
    public function verify_auth_fun(){
        $url = request()->controller().'/'.request()->action();
        $url = strtolower($url);
        if ($url == 'index/index'){
            return true;
        }
        $auth_fun = json_decode($this->auth_data['auth_fun']);
        if (is_array($auth_fun)){
            $auth_fun = $this->get_auth_funs($auth_fun);
            foreach ($auth_fun as $v){
                foreach ($v['role_res'] as $vo){
                    if ( $url == strtolower($vo['nav_url'])){
                            return true;
                        }
                }
            }
        }else{
            $auth_fun = $this->get_auth_fun($auth_fun);
            foreach ($auth_fun['role_res'] as $v){
                if ( $url == strtolower($v['nav_url'])){
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 资源权限认证
     */
    public function verify_auth_res(){

    }

    /**权限等级判断 判断之前需先 set_to_id
     * @return bool
     */
//    public function verify_auth_level(){
//        if ($this->to_level > $this->auth_data['auth_level']){
//            return true;
//        }
//        return false;
//    }
}

